package com.ryujung.backtracking;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/*
 * @lc app=leetcode.cn id=17 lang=java
 *
 * [17] 电话号码的字母组合
 *
 * https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/description/
 *
 * algorithms
 * Medium (53.22%)
 * Likes:    686
 * Dislikes: 0
 * Total Accepted:    106.2K
 * Total Submissions: 199.4K
 * Testcase Example:  '"23"'
 *
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。
 * 
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * 
 * 
 * 
 * 示例:
 * 
 * 输入："23"
 * 输出：["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
 * 
 * 
 * 说明:
 * 尽管上面的答案是按字典序排列的，但是你可以任意选择答案输出的顺序。
 * 
 */

// @lc code=start
class Solution {
    public Map<Character, String> letterMap = new HashMap<Character, String>() {
        {
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }
    };

    public List<String> letterCombinations(String digits) {
        List<String> res = helper(new LinkedList<String>(Collections.singletonList("")), digits);

        return res;
    }

    private List<String> helper(List<String> already, String left) {
        if (left == null || left.length() == 0) {
            return new LinkedList<String>();
        }
        int l = left.length();
        List<String> res = new LinkedList<String>();
        String mapString = letterMap.get(left.charAt(0));
        for (String s : already) {
            for (int i = 0; i < mapString.length(); i++) {
                res.add(s + mapString.charAt(i));
            }
        }

        if (l > 1) {
            res = helper(res, left.substring(1, l));
        }

        return res;
    }

    /**
     * 每次处理完一个数后，所有组合的string的长度都+1，
     * 可以循环使用一个List，将长度不到+1的移除，
     * 不断循环，直到遍历完所有的digits
     */
    public List<String> method(String digits) {
        String[] ss = { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };

        int l = digits.length();
        LinkedList<String> list = new LinkedList<String>(Collections.singletonList(""));

        for (int i = 0; i < digits.length(); i++) {
            //digits当前字符代表的数值
            int num = Character.getNumericValue(digits.charAt(i));

            //根据数值从数组中得到对应的string信息
            String mapStr = ss[num];

            while (list.peek().length() == i) {
                String before = list.remove();
                for (char c : mapStr.toCharArray()) {
                    list.add(before + c);
                }
            }
        }
        return list;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        String str = "23";

        // List<String> list = s.letterCombinations(str);
        List<String> list = s.method(str);

        System.out.println(list);

    }
}
// @lc code=end
